#!/usr/bin/env python

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from common import *

policy_file = join(BUILD_DIR, "src", "tests", "policy.acl")
broker_args = "--acl-file {0}".format(policy_file)

broker_port = start_broker("broker", broker_args, "--log-enable debug+:acl")
broker_i_port = start_broker("broker_i", broker_args, "--connection-limit-per-ip 2")
broker_u_port = start_broker("broker_u", broker_args, "--connection-limit-per-user 2")
broker_q_port = start_broker("broker_q", broker_args, "--max-queues-per-user 2")

run_broker_tests(broker_port, "-m acl",
                 "-Dport-i={0}".format(broker_i_port),
                 "-Dport-u={0}".format(broker_u_port),
                 "-Dport-q={0}".format(broker_q_port),
                 "-Dpolicy-file={0}".format(policy_file))

# Test interaction of authentication and link creation

broker_1_port = start_broker("broker_1")
broker_2_port = start_broker("broker_2")

configure_broker(broker_1_port, "add exchange topic fed.topic")
configure_broker(broker_2_port, "add exchange topic fed.topic")

connect_brokers("dynamic add",
                "localhost:{0}".format(broker_1_port),
                "localhost:{0}".format(broker_2_port),
                "fed.topic")

sasl_config_dir = join(BUILD_DIR, "src", "tests", "sasl_config")
broker_args = "--auth yes --sasl-config {0}".format(sasl_config_dir)

broker_3_port = start_broker("broker_3", broker_args, auth_disabled=False)
broker_4_port = start_broker("broker_4", broker_args, auth_disabled=False)

configure_broker(broker_3_port, "add exchange topic fed.topic")
configure_broker(broker_4_port, "add exchange topic fed.topic")

try:
    connect_brokers("dynamic add",
                    "localhost:{0}".format(broker_3_port),
                    "localhost:{0}".format(broker_4_port),
                    "fed.topic")

    fail("Broker with no ACLs but auth enabled did not deny link creation")
except:
    pass

check_results()
